package icom.icss.dao.impl;

import icom.icss.dao.UserDao;
import icom.icss.entity.User;
import icom.icss.utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImpl implements UserDao {
    /**
     * @param username
     * @param password
     * @return
     * @throws SQLException
     */
    public User selcetByUsernameAndPassword(String username, String password) throws SQLException {
        Connection connection = JDBCUtils.getConnection();

        String sql = "select * from admin where username=? and password=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);

        ResultSet resultSet = preparedStatement.executeQuery();
        User user = null;
        while (resultSet.next()) {
            user = new User();
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));

        }
        JDBCUtils.realease(resultSet, preparedStatement, connection);
        return user;


    }

    /**
     * @param username
     * @return
     * @throws SQLException
     */
    public boolean checkUsername(String username) throws SQLException {
        Connection connection = JDBCUtils.getConnection();

        String sql = "select * from admin where username=?";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        preparedStatement.setString(1, username);
        ResultSet resultSet = preparedStatement.executeQuery();
        User user = null;
        while (resultSet.next()) {
            JDBCUtils.realease(resultSet, preparedStatement, connection);
            return true;
        }

        return false;
    }

    /**
     * @param Index
     * @param count，每页的条数
     * @return
     * @throws Exception
     */
    public List<User> getUserListByLimit(int Index, int count) throws SQLException {
        Connection connection = JDBCUtils.getConnection();

        PreparedStatement preparedStatement = connection.prepareStatement("select * from admin limit ?,?");

        preparedStatement.setInt(1, Index);
        preparedStatement.setInt(2, count);
        ResultSet resultSet = preparedStatement.executeQuery();
        List<User> Userlist = new ArrayList<>();
        while (resultSet.next()) {
            User user = new User();
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
            Userlist.add(user);

        }
        JDBCUtils.realease(resultSet, preparedStatement, connection);
        return Userlist;
    }

    /**
     * 删除
     *
     * @param username
     */
    public void deleteUser(String username) throws SQLException {
        Connection connection = JDBCUtils.getConnection();



        PreparedStatement preparedStatement = connection.prepareStatement("delete from admin where username = ?");

        preparedStatement.setString(1,username);


        preparedStatement.executeUpdate();

        JDBCUtils.realease(null, preparedStatement, connection);
    }

    /**
     *
     * @param username
     * @throws SQLException
     */
    public void deleteAllUser(String username) throws SQLException {
        Connection connection = JDBCUtils.getConnection();



        PreparedStatement preparedStatement = connection.prepareStatement("delete from admin where username in (?)");

        preparedStatement.setString(1,username);


        preparedStatement.executeUpdate();

        JDBCUtils.realease(null, preparedStatement, connection);
    }

    /**
     *
     * @param username
     * @return
     */
    public User selectName(String username) throws SQLException {
        Connection connection = JDBCUtils.getConnection();

        String sql = "select * from admin where username=?";

        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        preparedStatement.setString(1, username);

        ResultSet resultSet = preparedStatement.executeQuery();
        User user=null;
        while (resultSet.next()) {
             user = new User();
            user.setUsername(resultSet.getString("username"));
            user.setPassword(resultSet.getString("password"));
        }
        JDBCUtils.realease(resultSet, preparedStatement, connection);
        return user;
    }

    /**
     *
     * @param user
     * @throws SQLException
     */
    public void updatePassword(User user) throws SQLException {
        Connection connection = JDBCUtils.getConnection();

        PreparedStatement preparedStatement = connection.prepareStatement("update admin set  password=? where username=?");


        preparedStatement.setString(1,user.getPassword());
        preparedStatement.setString(2,user.getUsername());

        preparedStatement.executeUpdate();

        JDBCUtils.realease(null,preparedStatement,connection);

    }

    /**
     * @return
     * @throws SQLException
     */
    public int getTotalCount() throws SQLException {
        //获取连接
        Connection connection = JDBCUtils.getConnection();
        //获取statement对象
        PreparedStatement preparedStatement = connection.prepareStatement("select count(*) from admin");
        //执行sql
        ResultSet resultSet = preparedStatement.executeQuery();
        int totalCount = 0;
        //封装结果集
        while (resultSet.next()) {
            totalCount = resultSet.getInt(1);
        }
        JDBCUtils.realease(resultSet, preparedStatement, connection);
        return totalCount;
    }

}
